# -*- coding: utf-8 -*-

from sqlalchemy import INTEGER, Column
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.asyncio import create_async_engine, AsyncEngine, AsyncSession

from settings import get_config

config = get_config()

Base = declarative_base()


class BaseModel(Base):
    __abstract__ = True
    id = Column(INTEGER(), primary_key=True)


bind: AsyncEngine = create_async_engine(
    config.DB_SETTINGS["dsn"],
    pool_size=config.DB_SETTINGS["pool_size"],
    pool_recycle=config.DB_SETTINGS["pool_recycle"],
    max_overflow=config.DB_SETTINGS["max_overflow"],
    pool_pre_ping=True,
    echo=config.SQLALCHEMY_ECHO,  # 是否输出SQL日志
)

Session = sessionmaker(
    bind=bind,
    class_=AsyncSession,
    expire_on_commit=False
)


def init_db() -> None:
    Base.metadata.drop_all(bind=bind)
    Base.metadata.create_all(bind=bind)


if __name__ == "__main__":
    from app.apps.users.models import *
    # from app.apps.switchs.models import *

    init_db()
